#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from google.appengine.api import users 
import webapp2
from google.appengine.ext import db

class Family (db.Model):
    user = db.UserProperty(required=True) 
    last_name = db.StringProperty (required=True)
    home_phone = db.StringProperty (required=True)
    enroll_date = db.DateTimeProperty(auto_now_add=True)
    privilage = db.StringProperty (required=True)

class MainHandler(webapp2.RequestHandler):
    def get(self):
        user = users.get_current_user()

        if user:
            #self.response.headers['Content-Type'] = 'text/plain' 
            
            self.response.out.write('Hello, ' + user.email() + '<p/>')
            self.response.out.write('Hello <em>%s</em>! [<a href="%s">sign out</a>]' % (
                user.nickname(), users.create_logout_url(self.request.uri))) 
            #self.response.out.write('user<p/>'+ str(dir(user)) + '<p/>')
            self.response.out.write("""<form action="/sign" method="post">
              <div>Last Name  : <input type="text" name="last_name"/></div>
              <div>Home Phone : <input type="text" name="home_phone"/></div>
              <div><input type="submit" value="login"></div>
              </form>""")
        else:
            self.redirect(users.create_login_url(self.request.uri))


class GreetHandler(webapp2.RequestHandler):
    def post(self):
        last_name = self.request.get ('last_name').capitalize()
        home_phone = self.request.get ('home_phone')
        user = users.get_current_user()
        user_rows = db.GqlQuery("SELECT * "
                               "FROM Family "
                               "WHERE user = :1 ", user).get ()
        #self.response.out.write('got rows %s - %s<p/>' % (str(user_rows.__dict__), dir(user_rows)))
        if user_rows is None:
            Family (user = user, last_name=last_name, home_phone=home_phone,
                    privilage='requested').put()
        else:
	    ur = user_rows
            self.response.out.write("Got the record of %s<p/>" % user.nickname())
            self.response.out.write("Home phone in record : %s<p/>" % ur.home_phone)
            self.response.out.write("last name in record : %s<p/>" % ur.last_name)
            self.response.out.write("Enroled in %s<p/>" % repr(ur.enroll_date))
            self.response.out.write("privilage : %s<p/>" % ur.privilage)
            
            if ur.privilage == 'requested':
                self.response.out.write("your reqest is being processed<p/>")
            elif ur.privilage == 'user':
                self.response.out.write("your reqest has been approved<p/>")
        self.response.out.write('Hi %s family!<p/><p/>' % last_name)
        self.response.out.write('Noted your phone # : %s' % home_phone)
        
app = webapp2.WSGIApplication([('/', MainHandler), ('/sign', GreetHandler)],
                              debug=True)
